﻿/*========================== 
 * @author 郑金泉
 * @desc 查询处理类
 * ========================= */
using System;
using System.Collections.Generic;
using System.Text;

using Obsidian.Edm;

namespace Obsidian.Data.Sql
{
    public class DbHandler
    {

        private IModel _model;
        protected List<OperateField> fieldList = new List<OperateField>();
        protected List<OrderByClause> orderByList = new List<OrderByClause>();
        protected Dictionary<string, JoinClause> dictJoinClauses = new Dictionary<string, JoinClause>();
        protected CompoundCondition whereCondition = new CompoundCondition();

        public IModel OperateModel 
        {
            get { return this._model; }
            set { this._model = value; }
        }

        #region 设置字段

        public DbHandler SetField(IModelField field)
        {
            if (field.GetDbField() != null)
            {
                OperateField of = new OperateField(field);
                this.fieldList.Add(of);
            }
            return this;
        }

        public DbHandler SetField(IModelField field, object value)
        {
            return SetField(field, value, OperType.Not);
        }

        public DbHandler SetField(IModelField field, object value, OperType operType)
        {
            if (field.GetDbField() != null)
            {
                OperateField of = new OperateField(field, value);
                of.OperType = operType;
                this.fieldList.Add(of);
            }
            return this;
        }

        public DbHandler SetFields(params IModelField[] fields)
        {
            foreach (IModelField field in fields)
                this.SetField(field);
            return this;
        }

        #endregion

        #region Join

        public JoinClause Join(IModel model)
        {
            JoinClause joinClause = new JoinClause(model, JoinType.Inner);
            return this.Join(joinClause);
        }

        public JoinClause LeftOuterJoin(IModel model)
        {
            JoinClause joinClause = new JoinClause(model, JoinType.LeftOuter);
            return this.Join(joinClause);
        }

        public JoinClause RightOuterJoin(IModel model)
        {
            JoinClause joinClause = new JoinClause(model, JoinType.RightOuter);
            return this.Join(joinClause);
        }

        private JoinClause Join(JoinClause joinClause)
        {
            if (dictJoinClauses.ContainsKey(joinClause.Model.DbTableName))
                throw new Exception("不能重复连接表:" + joinClause.Model.DbTableName);
            dictJoinClauses.Add(joinClause.Model.DbTableName, joinClause);
            return joinClause;
        }

        #endregion

        /// <summary>
        /// Where条件
        /// </summary>
        public CompoundCondition Where(ISqlCondition condition)
        {
            this.whereCondition.AddSubCondition(condition);
            return this.whereCondition;
        }

        public void CheckQueryInfo(QueryInfo qry)
        {
            if (qry.IsPaging.IsNull)
                throw new Exception("IsPaging属性不能为空!");

            if (qry.IsPaging.isTrue())
            {
                if (qry.PageSize.IsNull)
                    throw new Exception("分页情况下，页大小不能为空!");
                if (qry.PageSize.Value <= 0)
                    throw new Exception("分页情况下，页大小必须大于0!");
                if (qry.PageNo.Value <= 0)
                    throw new Exception("分页情况下，页码必须大于0!");
            }
        }

        protected string Escape(string str)
        {
            return Escape(this._model.GetDataBase().Type, str);
        }

        public static string Escape(DataBaseType dbType, string str)
        {
            string result = str;
            if (dbType == DataBaseType.SqlServer)
            {
                result = result.Replace("'", "''");
            }
            return result;
        }

    }
}
